/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import java.awt.Event;
import java.util.List;
import pojo.SanPhamCuaHang;
import dao.SanPhamCuaHangDAO;
import dao.CuaHangDAO;
import dao.SanPhamDAO;
import dao.LoaiSanPhamDAO;
import java.awt.Component;
import java.util.Date;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JList;
import javax.swing.JOptionPane;
import pojo.CuaHang;
import pojo.SanPham;
import pojo.LoaiSanPham;


/**
 *
 * @author TUYETQUAN
 */
public class QLSanPham_CuaHangGui extends javax.swing.JPanel {

    /**
     * Creates new form QLSanPham_CuaHangGui
     */
    private JErrorProvider errSP_CH;
    private List<SanPhamCuaHang>SanPham_CuaHangs;
    private List<CuaHang>cuaHangs;
    private List<SanPham>sanPhams;
    private List<LoaiSanPham>loaiSanPhams;
    public QLSanPham_CuaHangGui() throws Exception {
        initComponents();
        SetTabOrder();
        AutoLoad();
    }
    private void SetTabOrder(){
        IndexedFocusTraversalPolicy indexedFocusTraversalPolicy=new IndexedFocusTraversalPolicy();
        indexedFocusTraversalPolicy.addIndexedComponents(new Component[]{cmbMaCH,cmbMaLSP,cmbMaSP,txtTenCH,txtTenLSP,txtTenSP,txtSoLuong,txtMaNhap,btnClear,btnThem,btnXoa,btnSua});
        setFocusTraversalPolicy(indexedFocusTraversalPolicy);
    }
    public void AutoLoad()throws Exception{
        SanPham_CuaHangs =(List<SanPhamCuaHang>)SanPhamCuaHangDAO.LayDanhSach(SanPhamCuaHang.class, MainGUI.nguoiDung);
        //SanPham_CuaHangs =(List<SanPhamCuaHang>)SanPhamCuaHangDAO.LayDanhSachSanPhamCuaHang(SanPhamCuaHang.class, MainGUI.nguoiDung);
        Utility.CreateColumnsFromDB(SanPhamCuaHang.class,SanPham_CuaHangs,tbDanhSachSanPhamTheoCuaHang,new String []{"Mã","Tên Cửa Hàng ","Tên Sẩn Phẩm","Số Lượng"}, null);
        cuaHangs =(List<CuaHang>)CuaHangDAO.LayDanhSach(CuaHang.class, MainGUI.nguoiDung);
        Utility.SetDataSourceInCombo(cuaHangs, cmbMaCH);
        cmbMaCH.setRenderer(new DefaultListCellRenderer(){
            @Override
            public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
                super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
                if(value instanceof CuaHang){
                    CuaHang cH=(CuaHang)value;
                    setText(String.valueOf(cH.getId()));                    
                }               
                return this;
            }    
        });
        cmbMaCH.setSelectedIndex(-1);
        loaiSanPhams =(List<LoaiSanPham>)LoaiSanPhamDAO.LayDanhSach(LoaiSanPham.class,MainGUI.nguoiDung);
        Utility.SetDataSourceInCombo(loaiSanPhams, cmbMaLSP);
        cmbMaLSP.setRenderer(new DefaultListCellRenderer(){
            @Override
            public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus){
                super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
                if(value instanceof LoaiSanPham){
                    LoaiSanPham lsp =(LoaiSanPham)value;
                    setText(String.valueOf(lsp.getId()));
                }
                return this;
            }
        });
        cmbMaLSP.setSelectedIndex(-1);
        
        sanPhams =(List<SanPham>)SanPhamDAO.LayDanhSach(SanPham.class, MainGUI.nguoiDung);
        Utility.SetDataSourceInCombo(sanPhams, cmbMaSP);
        cmbMaSP.setRenderer(new DefaultListCellRenderer(){
            @Override
            public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus){
                super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
                if(value instanceof SanPham){
                    SanPham sp =(SanPham)value;                                                      
                    setText(String.valueOf(sp.getId()));            
                }
                return this;
            }
        });
        cmbMaSP.setSelectedIndex(-1);
        
      
       
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel6 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        cmbMaCH = new javax.swing.JComboBox();
        cmbMaLSP = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        cmbMaSP = new javax.swing.JComboBox();
        jPanel4 = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        txtTenCH = new javax.swing.JTextField();
        txtTenLSP = new javax.swing.JTextField();
        txtTenSP = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jPanel5 = new javax.swing.JPanel();
        jLabel7 = new javax.swing.JLabel();
        txtSoLuong = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        txtMaNhap = new javax.swing.JTextField();
        jPanel2 = new javax.swing.JPanel();
        btnThem = new javax.swing.JButton();
        btnXoa = new javax.swing.JButton();
        btnSua = new javax.swing.JButton();
        btnClear = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        tbDanhSachSanPhamTheoCuaHang = new org.jdesktop.swingx.JXTable();
        jPanel7 = new javax.swing.JPanel();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        txtMaCH = new javax.swing.JTextField();
        txtTenCH_Tim = new javax.swing.JTextField();
        txtMaSP = new javax.swing.JTextField();
        txtTenSP_Tim = new javax.swing.JTextField();
        jPanel3 = new javax.swing.JPanel();
        btnTim = new javax.swing.JButton();

        jLabel1.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
        jLabel1.setText("Mã Cửa Hàng");

        cmbMaCH.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
        cmbMaCH.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmbMaCHActionPerformed(evt);
            }
        });

        cmbMaLSP.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
        cmbMaLSP.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmbMaLSPActionPerformed(evt);
            }
        });

        jLabel3.setText("Mã Loại Sản Phẩm");

        jLabel2.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
        jLabel2.setText("Mã Sản Phẩm");

        cmbMaSP.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
        cmbMaSP.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmbMaSPActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel6Layout = new javax.swing.GroupLayout(jPanel6);
        jPanel6.setLayout(jPanel6Layout);
        jPanel6Layout.setHorizontalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel6Layout.createSequentialGroup()
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(cmbMaLSP, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(jPanel6Layout.createSequentialGroup()
                        .addGap(1, 1, 1)
                        .addComponent(cmbMaCH, 0, 51, Short.MAX_VALUE))
                    .addComponent(cmbMaSP, javax.swing.GroupLayout.Alignment.TRAILING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(5, 5, 5))
        );
        jPanel6Layout.setVerticalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(cmbMaCH, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(cmbMaLSP, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(cmbMaSP, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2)))
        );

        jLabel4.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
        jLabel4.setText("Tên Cửa Hàng");

        txtTenCH.setEditable(false);

        txtTenLSP.setEditable(false);

        txtTenSP.setEditable(false);

        jLabel6.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
        jLabel6.setText("Tên Sản Phẩm");

        jLabel5.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
        jLabel5.setText("Tên Loại Sản Phẩm");

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addGap(47, 47, 47)
                        .addComponent(txtTenCH, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
                        .addComponent(jLabel5)
                        .addGap(18, 18, 18)
                        .addComponent(txtTenLSP))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
                        .addComponent(jLabel6)
                        .addGap(46, 46, 46)
                        .addComponent(txtTenSP)))
                .addContainerGap())
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(txtTenCH, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtTenLSP, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel6)
                    .addComponent(txtTenSP, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        jLabel7.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
        jLabel7.setText("Số Lượng Nhập");

        txtSoLuong.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N

        jLabel8.setText("Mã Nhập");

        txtMaNhap.setEditable(false);

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel7)
                    .addComponent(jLabel8))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(txtSoLuong, javax.swing.GroupLayout.DEFAULT_SIZE, 50, Short.MAX_VALUE)
                    .addComponent(txtMaNhap))
                .addContainerGap(21, Short.MAX_VALUE))
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addGap(26, 26, 26)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(txtSoLuong, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(txtMaNhap, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        btnThem.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
        btnThem.setText("Thêm");
        btnThem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnThemActionPerformed(evt);
            }
        });

        btnXoa.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
        btnXoa.setText("Xóa");
        btnXoa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnXoaActionPerformed(evt);
            }
        });

        btnSua.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
        btnSua.setText("Sửa");
        btnSua.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSuaActionPerformed(evt);
            }
        });

        btnClear.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
        btnClear.setText("Clear");
        btnClear.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnClearActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(124, 124, 124)
                .addComponent(btnThem)
                .addGap(41, 41, 41)
                .addComponent(btnXoa)
                .addGap(46, 46, 46)
                .addComponent(btnSua)
                .addGap(43, 43, 43)
                .addComponent(btnClear)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnThem)
                    .addComponent(btnXoa)
                    .addComponent(btnSua)
                    .addComponent(btnClear))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(20, Short.MAX_VALUE)
                .addComponent(jPanel6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(2, 2, 2)
                .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jPanel4, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 0, Short.MAX_VALUE)
                    .addComponent(jPanel6, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        tbDanhSachSanPhamTheoCuaHang.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        tbDanhSachSanPhamTheoCuaHang.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
        tbDanhSachSanPhamTheoCuaHang.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tbDanhSachSanPhamTheoCuaHangMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(tbDanhSachSanPhamTheoCuaHang);

        jPanel7.setBorder(javax.swing.BorderFactory.createTitledBorder("Thông Tin Tìm Kiếm"));

        jLabel9.setText("Mã Cửa Hàng");

        jLabel10.setText("Tên Cửa Hàng");

        jLabel11.setText("Mã Sản Phẩm");

        jLabel12.setText("Tên Sản Phẩm");

        txtMaCH.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
        txtMaCH.setToolTipText("dữ liệu tìm là số nguyên");

        txtTenCH_Tim.setToolTipText("dữ liệu tìm là chuổi kí tự");

        txtMaSP.setToolTipText("dữ liệu tìm là số nguyên");

        txtTenSP_Tim.setToolTipText("dữ liệu tìm là chuổi kí tự");

        btnTim.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
        btnTim.setText("Tìm");
        btnTim.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnTimActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(btnTim)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addGap(0, 0, Short.MAX_VALUE)
                .addComponent(btnTim))
        );

        javax.swing.GroupLayout jPanel7Layout = new javax.swing.GroupLayout(jPanel7);
        jPanel7.setLayout(jPanel7Layout);
        jPanel7Layout.setHorizontalGroup(
            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel7Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel9)
                    .addComponent(jLabel10)
                    .addComponent(jLabel11)
                    .addComponent(jLabel12))
                .addGap(28, 28, 28)
                .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtTenSP_Tim, javax.swing.GroupLayout.DEFAULT_SIZE, 124, Short.MAX_VALUE)
                    .addComponent(txtMaSP)
                    .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(txtMaCH)
                        .addComponent(txtTenCH_Tim, javax.swing.GroupLayout.DEFAULT_SIZE, 124, Short.MAX_VALUE)))
                .addGap(18, 18, 18)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        jPanel7Layout.setVerticalGroup(
            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel7Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jPanel3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel7Layout.createSequentialGroup()
                        .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel9)
                            .addComponent(txtMaCH, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel10)
                            .addComponent(txtTenCH_Tim, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel11)
                            .addComponent(txtMaSP, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel12)
                            .addComponent(txtTenSP_Tim, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addGap(0, 23, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(0, 0, 0)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel7, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 288, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void tbDanhSachSanPhamTheoCuaHangMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbDanhSachSanPhamTheoCuaHangMouseClicked
        Object []temp =Utility.GetSelectedRows(tbDanhSachSanPhamTheoCuaHang, SanPham_CuaHangs);
        if(temp.length>0){
            SanPhamCuaHang sanPham_cuaHang =(SanPhamCuaHang)temp[0];
            SetDataSanPhamCuaHangControls(sanPham_cuaHang);            
            Utility.CreateColumnsFromDB(SanPhamCuaHang.class, SanPham_CuaHangs, tbDanhSachSanPhamTheoCuaHang,new String[]{"Mã","Tên Của Hàng","Tên Sản Phẩm ","Số Lượng"},null);
            errSP_CH.clear();
        }
    }//GEN-LAST:event_tbDanhSachSanPhamTheoCuaHangMouseClicked
//////
    public void cmbMaCHAction(){
        if(cmbMaCH.getModel().getSelectedItem()!=null)
        {
            CuaHang ch=(CuaHang)cmbMaCH.getModel().getSelectedItem();
            txtTenCH.setText(ch.getTenCuaHang());
        }
        else
        {
            txtTenCH.setText("");
        }
    }
    private void cmbMaCHActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmbMaCHActionPerformed
        cmbMaCHAction();
    }//GEN-LAST:event_cmbMaCHActionPerformed

    public void cmbMaLSPAction(){
        if(cmbMaLSP.getModel().getSelectedItem()!=null)
        {
            LoaiSanPham lsp=(LoaiSanPham)cmbMaLSP.getModel().getSelectedItem();            
            txtTenLSP.setText(lsp.getTenLoaiSanPham());              
        }
        else
        {
            txtTenLSP.setText("");         
        }
    }
    private void cmbMaLSPActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmbMaLSPActionPerformed
        
               cmbMaLSPAction();
    }//GEN-LAST:event_cmbMaLSPActionPerformed

    public void cmbMaSPAction(){
       if(cmbMaSP.getModel().getSelectedItem()!=null)
        {                   
            SanPham sp=(SanPham)cmbMaSP.getModel().getSelectedItem();
            txtTenSP.setText(sp.getTenSanPham());            
        }
        else
        {
            txtTenSP.setText("");
        }    
    }
    private void cmbMaSPActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmbMaSPActionPerformed
      
                  cmbMaSPAction();        
    }//GEN-LAST:event_cmbMaSPActionPerformed

    private void btnThemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnThemActionPerformed
        try{                
            if(txtSoLuong.getText().trim().isEmpty())
            {
                errSP_CH.setError(txtSoLuong, "required");
                return;
            }   
            CuaHang cHang =(CuaHang)cmbMaCH.getModel().getSelectedItem();
            SanPham sp =(SanPham)cmbMaSP.getModel().getSelectedItem();
           SanPhamCuaHang sP_cH = new SanPhamCuaHang(0,cHang,sp,Integer.valueOf(txtSoLuong.getText().trim()),false);                     
            SanPhamCuaHangDAO.ThemSanPham_CuaHang(sP_cH);            
            SanPham_CuaHangs.add(sP_cH);            
            Utility.CreateColumnsFromDB(SanPhamCuaHang.class, SanPham_CuaHangs, tbDanhSachSanPhamTheoCuaHang,new String[]{"Mã","Tên Của Hàng","Tên Sản Phẩm ","Số Lượng"},null);                          
            tbDanhSachSanPhamTheoCuaHang.changeSelection(tbDanhSachSanPhamTheoCuaHang.getModel().getRowCount()-1, 0, true, false);
            JOptionPane.showMessageDialog(this, "thêm thành công !");
        }catch(Exception ex){
            JOptionPane.showMessageDialog(this, "that bai");
        }
    }//GEN-LAST:event_btnThemActionPerformed
 private void SetDataSanPhamCuaHangControls(SanPhamCuaHang sanPham_cuaHang)
    {
        cmbMaCH.getModel().setSelectedItem(sanPham_cuaHang.getCuaHang().getId());
        cmbMaLSP.getModel().setSelectedItem(sanPham_cuaHang.getSanPham().getLoaiSanPham().getId());
        cmbMaSP.getModel().setSelectedItem(sanPham_cuaHang.getSanPham().getId());
//        cmbMaCHAction();
//        cmbMaSPAction();
//        cmbMaLSPAction();
        
        txtTenCH.setText(sanPham_cuaHang.getTenCuaHang());
        txtTenLSP.setText(sanPham_cuaHang.getSanPham().getTenLoaiSanPham());
        txtTenSP.setText(sanPham_cuaHang.getSanPham().getTenSanPham());
        txtSoLuong.setText(String.valueOf(sanPham_cuaHang.getSoLuong())); 
        txtMaNhap.setText(String.valueOf(sanPham_cuaHang.getId()));
    }
    private void btnSuaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSuaActionPerformed
        Object []temp =Utility.GetSelectedRows(tbDanhSachSanPhamTheoCuaHang,SanPham_CuaHangs);
        if(temp.length!=0){           
            try{
                int vt =SanPham_CuaHangs.indexOf(temp[0]);                
                SanPhamCuaHang spch =SanPham_CuaHangs.get(vt); 
                spch.setSoLuong(Integer.valueOf(txtSoLuong.getText().trim()));                            
                SanPhamCuaHangDAO.SuaSanPham_CuaHang(spch);
                Utility.CreateColumnsFromDB(SanPhamCuaHang.class, SanPham_CuaHangs, tbDanhSachSanPhamTheoCuaHang,new String[]{"Mã","Tên Của Hàng","Tên Sản Phẩm ","Số Lượng"},null);                              
                JOptionPane.showMessageDialog(this, "Sửa thành công");
            }catch(Exception ex){
                JOptionPane.showMessageDialog(this, ex.getMessage());
                clearall();
                  return;
            }            
        }
        else{
           JOptionPane.showMessageDialog(this, "hãy chọn đối tượng cần sửa bằng cách click lên đối tượng dưới lưới");
       }
    }//GEN-LAST:event_btnSuaActionPerformed

    private void btnXoaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnXoaActionPerformed
        Object []temp =Utility.GetSelectedRows(tbDanhSachSanPhamTheoCuaHang, SanPham_CuaHangs);          
       if(temp.length!=0){           
            if(JOptionPane.showConfirmDialog(this, "hãy chắn rằng muốn xóa cửa hàng này ", "Question", JOptionPane.YES_NO_OPTION)==0){
                try{                                                   
                    int vt =SanPham_CuaHangs.indexOf(temp[0]);                          
                    SanPhamCuaHang spch =SanPham_CuaHangs.get(vt);
                    spch.setIsDelete(true);                         
                    SanPhamCuaHangDAO.XoaSanPham_CuaHang(temp);                        
                    Utility.RemoveSelectedRows(SanPhamCuaHang.class,tbDanhSachSanPhamTheoCuaHang,SanPham_CuaHangs,new String[]{"Mã","Tên Của Hàng","Tên Sản Phẩm ","Số Lượng"},null);                                               
                   // Utility.ClearControlInPanel(null);                  
                    JOptionPane.showMessageDialog(this,"xóa thành công");                        
                } 
                catch(Exception ex){
                    JOptionPane.showMessageDialog(this, "không xóa được");
                    return;
                }                    
            }            
       }
       else{
           JOptionPane.showMessageDialog(this, "hãy chọn đối tượng cần xóa bằng cách click lên đối tượng dưới lưới");
       }
    }//GEN-LAST:event_btnXoaActionPerformed

    public void clearall(){
       cmbMaCH.setSelectedIndex(-1);
       cmbMaLSP.setSelectedIndex(-1);
       cmbMaSP.setSelectedIndex(-1);
       txtTenCH.setText("");
       txtSoLuong.setText("");
       txtMaNhap.setText("");
       txtMaCH.setText("");
       txtTenCH_Tim.setText("");
       txtMaSP.setText("");
      if( txtTenLSP.getText().isEmpty()==false){
           txtTenLSP.setText("");
      }
      if(txtTenSP.getText().isEmpty()==false){
          txtTenSP.setText("");
      }
      if(txtTenCH.getText().isEmpty()==false){
          txtTenCH.setText("");
      }
       //Utility.RemoveSelectedRows(SanPhamCuaHang.class, tbDanhSachSanPhamTheoCuaHang, cuaHangs, new String[]{"Mã","Tên Của Hàng","Tên Sản Phẩm ","Số Lượng"},null);
    }
    private void btnClearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnClearActionPerformed
      clearall();
    }//GEN-LAST:event_btnClearActionPerformed

    private void btnTimActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnTimActionPerformed
      try {
            // TODO add your handling code here:
            Integer maCuaHang = txtMaCH.getText().isEmpty() ? null : Integer.parseInt(txtMaCH.getText());
            Integer maSanPham = txtMaSP.getText().isEmpty() ? null : Integer.parseInt(txtMaSP.getText());
            SanPham_CuaHangs = SanPhamCuaHangDAO.TimKiemSanPhamCuaHang(maCuaHang,txtTenCH_Tim.getText().trim(), maSanPham,txtTenSP_Tim.getText().trim(), false);
            Utility.CreateColumnsFromDB(SanPhamCuaHang.class, SanPham_CuaHangs, tbDanhSachSanPhamTheoCuaHang, new String[]{"Mã","Tên Của Hàng","Tên Sản Phẩm ","Số Lượng"},null);
            if (SanPham_CuaHangs.isEmpty()) {
                JOptionPane.showMessageDialog(this, "Không tìm thấy");
            }
        } catch (Exception ex) {
        //    logger.error(new Date().toString(), ex);
            JOptionPane.showMessageDialog(this, "dữ liệu tìm không hợp lệ");
        }
    }//GEN-LAST:event_btnTimActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnClear;
    private javax.swing.JButton btnSua;
    private javax.swing.JButton btnThem;
    private javax.swing.JButton btnTim;
    private javax.swing.JButton btnXoa;
    private javax.swing.JComboBox cmbMaCH;
    private javax.swing.JComboBox cmbMaLSP;
    private javax.swing.JComboBox cmbMaSP;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JPanel jPanel7;
    private javax.swing.JScrollPane jScrollPane2;
    private org.jdesktop.swingx.JXTable tbDanhSachSanPhamTheoCuaHang;
    private javax.swing.JTextField txtMaCH;
    private javax.swing.JTextField txtMaNhap;
    private javax.swing.JTextField txtMaSP;
    private javax.swing.JTextField txtSoLuong;
    private javax.swing.JTextField txtTenCH;
    private javax.swing.JTextField txtTenCH_Tim;
    private javax.swing.JTextField txtTenLSP;
    private javax.swing.JTextField txtTenSP;
    private javax.swing.JTextField txtTenSP_Tim;
    // End of variables declaration//GEN-END:variables
}
